package com.empty.oredermenu.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.maps.model.MyTrafficStyle;
import com.amap.api.maps.model.Poi;
import com.amap.api.navi.AmapNaviPage;
import com.amap.api.navi.AmapNaviParams;
import com.amap.api.navi.INaviInfoCallback;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RidePath;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkRouteResult;
import com.empty.oredermenu.R;
import com.empty.oredermenu.adapter.BusResultListAdapter;
import com.empty.oredermenu.naviactivity.RideRouteCalculateActivity;
import com.empty.oredermenu.naviactivity.WalkRouteCalculateActivity;
import com.empty.oredermenu.orly.DrivingRouteOverlay;
import com.empty.oredermenu.orly.RideRouteOverlay;
import com.empty.oredermenu.orly.WalkRouteOverlay;
import com.empty.oredermenu.routeactivity.DriveRouteDetailActivity;
import com.empty.oredermenu.routeactivity.RideRouteDetailActivity;
import com.empty.oredermenu.routeactivity.WalkRouteDetailActivity;
import com.empty.oredermenu.utils.AMapUtil;
import com.empty.oredermenu.utils.TTSController;

/**
 * Created by empty cup on 2017/7/12.
 */

public class AMapViewActivity extends Activity implements AMap.OnMyLocationChangeListener,AMap.InfoWindowAdapter,
        AMap.OnMapClickListener,AMapLocationListener,INaviInfoCallback,RouteSearch.OnRouteSearchListener {

    private MapView mMapView = null;
//    private MyLocationStyle myLocationStyle;
    /**
     * AMap 类是地图的控制器类，用来操作地图。它所承载的工作包括：地图图层切换（如卫星图、黑夜地图）、
     * 改变地图状态（地图旋转角度、俯仰角、中心点坐标和缩放级别）、添加点标记（Marker）
     * 、绘制几何图形(Polyline、Polygon、Circle)、各类事件监听(点击、手势等)等，
     * AMap 是地图 SDK 最重要的核心类，诸多操作都依赖它完成。
     */
    private AMap aMap;
    private UiSettings mUiSettings;

//    private LocationSource.OnLocationChangedListener mListener;
//    AMapLocationClient mlocationClient;
//    AMapLocationClientOption mLocationOption;

    private static final int STROKE_COLOR = Color.argb(180, 3, 145, 255);
    private static final int FILL_COLOR = Color.argb(10, 0, 0, 180);




    private View infoWindow = null;
    private MyLocationStyle myLocationStyle;

    /**
     *
     *地图路径选择规划
     */
    private ImageView route_drive;
    private ImageView route_bus;
    private ImageView route_walk;
    private LinearLayout mBusResultLayout;
    private ListView mBusResultList;

    private RelativeLayout mBottomLayout;
    private TextView mRotueTimeDes, mRouteDetailDes;

    private RouteSearch mRouteSearch;
    private DriveRouteResult mDriveRouteResult;
    private BusRouteResult mBusRouteResult;
    private WalkRouteResult mWalkRouteResult;
    private RideRouteResult mRideRouteResult;

    private LatLonPoint mStartPoint_bus = new LatLonPoint(31.446941, 104.692677);//起点
    private LatLonPoint mEndPoint_bus = new LatLonPoint(31.513189, 104.703435);//终点，
//    public static final LatLng CHENGDU = new LatLng(30.679879, 104.064855);// 成都市经纬度
LatLng epoint = new LatLng(39.935039, 116.492446);
//
    private LatLonPoint mStartPoint = new LatLonPoint(31.446941, 104.692677);//起点，116.335891,39.942295
    private LatLonPoint mEndPoint = new LatLonPoint(39.935039, 116.492446);//终点，116.481288,39.995576

    private final int ROUTE_TYPE_BUS = 1;
    private final int ROUTE_TYPE_DRIVE = 2;
    private final int ROUTE_TYPE_WALK = 3;
    private final int ROUTE_TYPE_CROSSTOWN = 4;
    private final int ROUTE_TYPE_RIDE = 5;

    private ProgressDialog progDialog = null;// 搜索时进度条
    private String mCurrentCityName = "绵阳";


    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;
//
    TTSController amapTTSController;//语音 开始
    private Button button_starNavi;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 不显示程序的标题栏
        setContentView(R.layout.activity_mapview);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.map);
//        在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        mMapView.onCreate(savedInstanceState);
        amapTTSController = TTSController.getInstance(getApplicationContext());
        amapTTSController.init();
        //设置SDK 自带定位消息监听
        init_amap();

        init();

    }

    private void init(){

        button_starNavi = (Button) findViewById(R.id.button_starNavi);

        mBottomLayout = (RelativeLayout) findViewById(R.id.bottom_layout);
        mRotueTimeDes = (TextView) findViewById(R.id.firstline);
        mRouteDetailDes = (TextView) findViewById(R.id.secondline);

        route_drive = (ImageView) findViewById(R.id.route_drive);
        route_bus = (ImageView) findViewById(R.id.route_bus);
        route_walk = (ImageView) findViewById(R.id.route_walk);

        mBusResultLayout = (LinearLayout) findViewById(R.id.bus_result);
        mBusResultList = (ListView) findViewById(R.id.bus_result_list);

        mRouteSearch = new RouteSearch(this);
        mRouteSearch.setRouteSearchListener(this);

        init_Location();

    }

    /**
     * AMap 实现 和一些设置
     */
    private void init_amap(){
        if (aMap == null) {
            aMap = mMapView.getMap();
            mUiSettings = aMap.getUiSettings();

            aMap.setOnMyLocationChangeListener(this);
            setUpMap();
        }


        aMap.setInfoWindowAdapter(this);//AMap类中

        aMap.setOnMapClickListener(this);


        mUiSettings.setTiltGesturesEnabled(false);// 设置地图是否可以倾斜
        aMap.setTrafficEnabled(true);// 显示实时交通状况
        aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 矢量地图模式
    }

    /**
     * 设置 实时交通的 路线颜色
     */
    private void Traffic_set(){
        //自定义实时交通信息的颜色样式
        MyTrafficStyle myTrafficStyle = new MyTrafficStyle();
        myTrafficStyle.setSeriousCongestedColor(0xff92000a);
        myTrafficStyle.setCongestedColor(0xffea0312);
        myTrafficStyle.setSlowColor(0xffff7508);
        myTrafficStyle.setSmoothColor(0xff00a209);
        aMap.setMyTrafficStyle(myTrafficStyle);
    }

    /**
     * 开始导航
     * @param indexNavi
     */
    private void start_Naci(final int indexNavi){
        button_starNavi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (indexNavi){
                    case ROUTE_TYPE_DRIVE:
                        LatLng epoint = new LatLng(31.513189, 104.703435);
                        Poi epoi = new Poi("绵阳", epoint, "成都");
                        AmapNaviPage.getInstance().showRouteActivity(getApplicationContext(), new AmapNaviParams(epoi), AMapViewActivity.this);
                        break;
                    case ROUTE_TYPE_WALK:
                        startActivity(new Intent(AMapViewActivity.this, WalkRouteCalculateActivity.class));
                        break;
                    case ROUTE_TYPE_RIDE:
                        startActivity(new Intent(AMapViewActivity.this, RideRouteCalculateActivity.class));
                        break;
                }
            }
        });
    }

    /**
     * 定位 配置
     */
    private void init_Location(){
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();

        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //获取一次定位结果：
        //该方法默认为false。
        mLocationOption.setOnceLocation(true);

       //获取最近3s内精度最高的一次定位结果：
       //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。
        // 如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
        mLocationOption.setOnceLocationLatest(true);
        //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
        mLocationOption.setInterval(1000);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否强制刷新WIFI，默认为true，强制刷新。
        mLocationOption.setWifiActiveScan(false);
        //设置是否允许模拟位置,默认为true，允许模拟位置
        mLocationOption.setMockEnable(false);
        //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
        mLocationOption.setHttpTimeOut(20000);
        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(false);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();

    }
    /**
     * 公交路线搜索
     */
    public void onBusClick(View view) {
        searchRouteResult(ROUTE_TYPE_BUS, RouteSearch.BusDefault);
        mMapView.setVisibility(View.GONE);
        mBusResultLayout.setVisibility(View.VISIBLE);
    }
    /**
     * 驾车路线搜索
     */
    public void onDriveClick(View view) {
        searchRouteResult(ROUTE_TYPE_DRIVE, RouteSearch.DrivingDefault);
        mMapView.setVisibility(View.VISIBLE);
        mBusResultLayout.setVisibility(View.GONE);
    }

    /**
     * 步行路线搜索
     */
    public void onWalkClick(View view) {
        searchRouteResult(ROUTE_TYPE_WALK, RouteSearch.WalkDefault);
        mMapView.setVisibility(View.VISIBLE);
        mBusResultLayout.setVisibility(View.GONE);
    }

    /**
     * 跨城公交路线搜索
     */
    public void onCrosstownBusClick(View view) {
        searchRouteResult(ROUTE_TYPE_CROSSTOWN, RouteSearch.BusDefault);
        mMapView.setVisibility(View.GONE);
        mBusResultLayout.setVisibility(View.VISIBLE);
    }

    /**
     * 骑行路线搜索
     */
    public void onRideClick(View view) {
        searchRouteResult(ROUTE_TYPE_RIDE, RouteSearch.RidingDefault);
        mMapView.setVisibility(View.VISIBLE);
        mBusResultLayout.setVisibility(View.GONE);
    }

    /**
     * 设置一些amap的属性
     */
    private void setUpMap() {
        aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        setupLocationStyle();
    }

    /**
     * 设置自定义定位蓝点  定位蓝点的 操作
     */
    private void setupLocationStyle(){
        // 自定义系统定位蓝点
        myLocationStyle = new MyLocationStyle();
        // 自定义定位蓝点图标
//        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.
//                fromResource(null));
        // 自定义精度范围的圆形边框颜色
        myLocationStyle.strokeColor(STROKE_COLOR);
        //自定义精度范围的圆形边框宽度
        myLocationStyle.strokeWidth(5);
        // 设置圆形的填充颜色
        myLocationStyle.radiusFillColor(FILL_COLOR);
        // 将自定义的 myLocationStyle 对象添加到地图上
//        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW) ;
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE) ;
//        aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE));
//        myLocationStyle.myLocationType(myLocationStyle) ;

//        myLocationStyle.showMyLocation(false);

        aMap.setMyLocationStyle(myLocationStyle);
    }

    /**
     * 设置地图的Marker
     * 设置 标记点的内容  和标题
     * 如果有 很多歌Marker 就要实例化一个数组 来进行 Marker 加载
     * @param lat
     * @param lon
     */
    private void canvce_MapMarker(double lat,double lon){
//        LatLng latLng = new LatLng(lat,lon);
//        final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).title("绵阳").snippet("DefaultMarker")
//        .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
//                .decodeResource(getResources(),R.mipmap.qcn))));


        setfromandtoMarker();


//        MarkerOptions markerOption = new MarkerOptions();
////        markerOption.position(SyncStateContract.Constants.XIAN);
////        markerOption.title("西安市").snippet("西安市：34.341568, 108.940174");
//
//        markerOption.draggable(true);//设置Marker可拖动
//        markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
//                .decodeResource(getResources(),R.mipmap.qcn)));
//        // 将Marker设置为贴地显示，可以双指下拉地图查看效果
//        markerOption.setFlat(true);//设置marker平贴地图效果
//        aMap.addMarker(markerOption);
    }
//
    private void setfromandtoMarker() {
        aMap.addMarker(new MarkerOptions()
                .position(convertToLatLng(mStartPoint_bus))
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.qcn)));
        Marker marker = aMap.addMarker(new MarkerOptions()
                .position(convertToLatLng(mEndPoint_bus))
                .title("详情").icon(BitmapDescriptorFactory.fromResource(R.mipmap.qcl)));
        marker.showInfoWindow();
        marker_OnClick();
    }

    /**
     * 把LatLonPoint对象转化为LatLon对象
     */
    public static LatLng convertToLatLng(LatLonPoint latLonPoint) {
        return new LatLng(latLonPoint.getLatitude(), latLonPoint.getLongitude());
    }

    /**
     * 定义 Marker 的点击监听
     */
    private void marker_OnClick(){
        // 定义 Marker 点击事件监听
        AMap.OnMarkerClickListener markerClickListener = new AMap.OnMarkerClickListener() {
            // marker 对象被点击时回调的接口
            // 返回 true 则表示接口已响应事件，否则返回false
            @Override
            public boolean onMarkerClick(Marker marker) {

                return false;
            }
        };
     // 绑定 Marker 被点击事件
        aMap.setOnMarkerClickListener(markerClickListener);
    }

    /**
     * 定义 infowindow 点击监听接口
     */
    private void Info_WindowMarker(){
        AMap.OnInfoWindowClickListener listener = new AMap.OnInfoWindowClickListener() {

            @Override
            public void onInfoWindowClick(Marker arg0) {

                arg0.setTitle("infowindow clicked");
            }
        };
       //绑定信息窗点击事件
        aMap.setOnInfoWindowClickListener(listener);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
//        if(null != mLocationClient){
//            mLocationClient.onDestroy();
//        }
//        amapTTSController.destroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }


    /**
     *
     * @param location
     */

    @Override
    public void onMyLocationChange(Location location) {
    }

    /**
     * 以下就是 Infowindow  自定义布局  回调
     * @param marker
     * @return
     */

    @Override
    public View getInfoWindow(Marker marker) {
        if(infoWindow == null) {
            infoWindow = LayoutInflater.from(this).inflate(
                    R.layout.map_infowindow, null);
        }
//        marker.showInfoWindow();
        //修改 自定义中的内容
//        render(marker, infoWindow);
        return infoWindow;
    }

    @Override
    public View getInfoContents(Marker marker) {
        if(infoWindow == null) {
            infoWindow = LayoutInflater.from(this).inflate(
                    R.layout.map_infowindow, null);
        }

        //修改 自定义中的内容
        return infoWindow;
    }

    public void render(Marker marker, View view) {
//如果想修改自定义Infow中内容，请通过view找到它并修改
    }

    /**
     * 以下就是 路径规划的 回调
     * @param busRouteResult
     * @param i
     */

    /**
     * 开始搜索路径规划方案
     */
    public void searchRouteResult(int routeType, int mode) {
//        if (mStartPoint == null) {
//            ToastUtil.show(mContext, "起点未设置");
//            return;
//        }
//        if (mEndPoint == null) {
//            ToastUtil.show(mContext, "终点未设置");
//        }
        showProgressDialog();
        final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
                mStartPoint_bus, mEndPoint_bus);
//        final RouteSearch.FromAndTo fromAndTo_point = new RouteSearch.FromAndTo(
//                mStartPoint, mEndPoint);

        if (routeType == ROUTE_TYPE_BUS) {// 公交路径规划

            RouteSearch.BusRouteQuery query = new RouteSearch.BusRouteQuery(fromAndTo, mode,
                    mCurrentCityName, 0);// 第一个参数表示路径规划的起点和终点，第二个参数表示公交查询模式，第三个参数表示公交查询城市区号，第四个参数表示是否计算夜班车，0表示不计算
            mRouteSearch.calculateBusRouteAsyn(query);// 异步路径规划公交模式查询

        } else if (routeType == ROUTE_TYPE_DRIVE) {// 驾车路径规划

            RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, mode, null,
                    null, "");// 第一个参数表示路径规划的起点和终点，第二个参数表示驾车模式，第三个参数表示途经点，第四个参数表示避让区域，第五个参数表示避让道路
            mRouteSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询

        } else if (routeType == ROUTE_TYPE_WALK) {// 步行路径规划

            RouteSearch.WalkRouteQuery query = new RouteSearch.WalkRouteQuery(fromAndTo, mode);
            mRouteSearch.calculateWalkRouteAsyn(query);// 异步路径规划步行模式查询

        } else if (routeType == ROUTE_TYPE_CROSSTOWN) {

            RouteSearch.FromAndTo fromAndTo_bus = new RouteSearch.FromAndTo(
                    mStartPoint_bus, mEndPoint_bus);
            RouteSearch.BusRouteQuery query = new RouteSearch.BusRouteQuery(fromAndTo_bus, mode,
                    "呼和浩特市", 0);// 第一个参数表示路径规划的起点和终点，第二个参数表示公交查询模式，第三个参数表示公交查询城市区号，第四个参数表示是否计算夜班车，0表示不计算
            query.setCityd("农安县");
            mRouteSearch.calculateBusRouteAsyn(query);// 异步路径规划公交模式查询

        }else if (routeType == ROUTE_TYPE_RIDE){

            final RouteSearch.FromAndTo fromAndTo_rid = new RouteSearch.FromAndTo(
                    mStartPoint_bus, mEndPoint_bus);
            if (routeType == ROUTE_TYPE_RIDE) {// 骑行路径规划
                RouteSearch.RideRouteQuery query = new RouteSearch.RideRouteQuery(fromAndTo_rid, mode);
                mRouteSearch.calculateRideRouteAsyn(query);// 异步路径规划骑行模式查询
            }
        }
    }

    @Override
    public void onBusRouteSearched(BusRouteResult result, int errorCode) {
        //公交路线
        dissmissProgressDialog();
        mBottomLayout.setVisibility(View.GONE);
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    mBusRouteResult = result;
                    BusResultListAdapter mBusResultListAdapter = new BusResultListAdapter(this, mBusRouteResult);
                    mBusResultList.setAdapter(mBusResultListAdapter);
                } else if (result != null && result.getPaths() == null) {
//                    ToastUtil.show(mContext, R.string.no_result);
                }
            } else {
//                ToastUtil.show(mContext, R.string.no_result);
            }
        } else {
//            ToastUtil.showerror(this.getApplicationContext(), errorCode);
        }
    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {
        //自驾路线
        dissmissProgressDialog();
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    mDriveRouteResult = result;
                    final DrivePath drivePath = mDriveRouteResult.getPaths()
                            .get(0);
                    DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
                            this, aMap, drivePath,
                            mDriveRouteResult.getStartPos(),
                            mDriveRouteResult.getTargetPos(), null);
                    drivingRouteOverlay.setNodeIconVisibility(false);//设置节点marker是否显示
                    drivingRouteOverlay.setIsColorfulline(true);//是否用颜色展示交通拥堵情况，默认true
                    drivingRouteOverlay.removeFromMap();
                    drivingRouteOverlay.addToMap();
                    drivingRouteOverlay.zoomToSpan();
                    mBottomLayout.setVisibility(View.VISIBLE);
                    int dis = (int) drivePath.getDistance();
                    int dur = (int) drivePath.getDuration();
                    String des = AMapUtil.getFriendlyTime(dur)+"("+AMapUtil.getFriendlyLength(dis)+")";
                    mRotueTimeDes.setText(des);
                    mRouteDetailDes.setVisibility(View.VISIBLE);
                    int taxiCost = (int) mDriveRouteResult.getTaxiCost();
                    mRouteDetailDes.setText("过路费"+taxiCost+"元");
                    mBottomLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(AMapViewActivity.this,
                                    DriveRouteDetailActivity.class);
                            intent.putExtra("drive_path", drivePath);
                            intent.putExtra("drive_result",
                                    mDriveRouteResult);
                            startActivity(intent);
                        }
                    });
                    start_Naci(ROUTE_TYPE_DRIVE);
                } else if (result != null && result.getPaths() == null) {
//                    ToastUtil.show(mContext, R.string.no_result);
                }

            } else {
//                ToastUtil.show(mContext, R.string.no_result);
            }
        } else {
//            ToastUtil.showerror(this.getApplicationContext(), errorCode);
        }
    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult result, int errorCode) {
        //步行路线
        dissmissProgressDialog();
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    mWalkRouteResult = result;
                    final WalkPath walkPath = mWalkRouteResult.getPaths()
                            .get(0);
                    WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(
                            this, aMap, walkPath,
                            mWalkRouteResult.getStartPos(),
                            mWalkRouteResult.getTargetPos());
                    walkRouteOverlay.removeFromMap();
                    walkRouteOverlay.addToMap();
                    walkRouteOverlay.zoomToSpan();
                    mBottomLayout.setVisibility(View.VISIBLE);
                    int dis = (int) walkPath.getDistance();
                    int dur = (int) walkPath.getDuration();
                    String des = AMapUtil.getFriendlyTime(dur)+"("+AMapUtil.getFriendlyLength(dis)+")";
                    mRotueTimeDes.setText(des);
                    mRouteDetailDes.setVisibility(View.GONE);
                    mBottomLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(AMapViewActivity.this,
                                    WalkRouteDetailActivity.class);
                            intent.putExtra("walk_path", walkPath);
                            intent.putExtra("walk_result",
                                    mWalkRouteResult);
                            startActivity(intent);
                        }
                    });
                    start_Naci(ROUTE_TYPE_WALK);
                } else if (result != null && result.getPaths() == null) {
//                    ToastUtil.show(mContext, R.string.no_result);
                }

            } else {
//                ToastUtil.show(mContext, R.string.no_result);
            }
        } else {
//            ToastUtil.showerror(this.getApplicationContext(), errorCode);
        }
    }

    @Override
    public void onRideRouteSearched(RideRouteResult result, int errorCode) {
        //骑行路线
        dissmissProgressDialog();
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    mRideRouteResult = result;
                    final RidePath ridePath = result.getPaths()
                            .get(0);
                    RideRouteOverlay rideRouteOverlay = new RideRouteOverlay(
                            this, aMap, ridePath,
                            mRideRouteResult.getStartPos(),
                            mRideRouteResult.getTargetPos());
                    rideRouteOverlay.removeFromMap();
                    rideRouteOverlay.addToMap();
                    rideRouteOverlay.zoomToSpan();
                    mBottomLayout.setVisibility(View.VISIBLE);
                    int dis = (int) ridePath.getDistance();
                    int dur = (int) ridePath.getDuration();
                    String des = AMapUtil.getFriendlyTime(dur)+"("+AMapUtil.getFriendlyLength(dis)+")";
                    mRotueTimeDes.setText(des);
                    mRouteDetailDes.setVisibility(View.GONE);
                    mBottomLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(AMapViewActivity.this,
                                    RideRouteDetailActivity.class);
                            intent.putExtra("ride_path", ridePath);
                            intent.putExtra("ride_result",
                                    mRideRouteResult);
                            startActivity(intent);
                        }
                    });
                    start_Naci(ROUTE_TYPE_RIDE);

                } else if (result != null && result.getPaths() == null) {
//                    ToastUtil.show(mContext, R.string.no_result);
                }
            } else {
//                ToastUtil.show(mContext, R.string.no_result);
            }
        } else {
//            ToastUtil.showerror(this.getApplicationContext(), errorCode);
        }
    }

    /**
     * 以上就是 路径规划的 回调
     */

    @Override
    public void onMapClick(LatLng latLng) {

    }

    /**
     * 显示进度框
     */
    private void showProgressDialog() {
//        if (progDialog == null)
//            progDialog = new ProgressDialog(this);
//        progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//        progDialog.setIndeterminate(false);
//        progDialog.setCancelable(true);
//        progDialog.setMessage("正在搜索");
//        progDialog.show();
    }

    /**
     * 隐藏进度框
     */
    private void dissmissProgressDialog() {
//        if (progDialog != null) {
//            progDialog.dismiss();
//        }
    }

    /**
     * 获取定位
     * @param amapLocation
     */
    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
            //可在其中解析amapLocation获取相应内容。
                Log.v("thismap","   可在其中解析amapLocation获取相应内容。       ");

                if (aMap != null){
//                    LatLonPoint mStartPoint_bus = new LatLonPoint(amapLocation.getLatitude(), amapLocation.getLongitude());//起点
//                    aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE));
//                    myLocationStyle.anchor(, amapLocation.getLongitude());
//                    LatLng latLng = new LatLng(amapLocation.getLatitude(),
//                            amapLocation.getLongitude());
                    LatLng latLng = new LatLng(31.513189, 104.703435);
//                    aMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(
//                            AMapUtil.convertToLatLng(mStartPoint_bus), 16, 30, 30)));
                    aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
                    aMap.moveCamera(CameraUpdateFactory.zoomTo(16));
//                    aMap.animateCamera(CameraUpdateFactory.changeLatLng(latLng));

                }
                canvce_MapMarker(amapLocation.getLatitude(),amapLocation.getLongitude());

            }else {
                //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                Log.e("AmapError","location Error, ErrCode:"
                        + amapLocation.getErrorCode() + ", errInfo:"
                        + amapLocation.getErrorInfo());
            }
        }
    }

    /**
     * 一下就是 实现 导航接口
     * 点击Markers 实现导航
     */
    @Override
    public void onInitNaviFailure() {

    }

    @Override
    public void onGetNavigationText(String s) {
//        amapTTSController.onGetNavigationText(0,s);
    }

    @Override
    public void onLocationChange(AMapNaviLocation aMapNaviLocation) {

    }

    @Override
    public void onArriveDestination(boolean b) {

    }

    @Override
    public void onStartNavi(int i) {

    }

    @Override
    public void onCalculateRouteSuccess(int[] ints) {

    }

    @Override
    public void onCalculateRouteFailure(int i) {

    }

    @Override
    public void onStopSpeaking() {
//        amapTTSController.stopSpeaking();
    }
}
